package Classes.Tanks 
{
	import Classes.Collisions.BaseCollision;
	import flash.geom.Point;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.Entity;
	/**
	 * ...
	 * @author Alex
	 */
	public class EnemyTank extends Tank
	{
		
		public function EnemyTank(startLocation:Point) 
		{
			super(startLocation);
			type = ObjectTypes.ENEMY_TANK;
			var image:Image = new Image(TankImageList.GFX_ENEMY_TANK);
			setImage(image);
			setHitbox(image.width, image.height, x, y);
			m_status = TankStatus.STOPPED;
		}
			
		
		public function moveControl():void
		{
			if (m_status == TankStatus.STOPPED)
				{
					moveInOppositeDirection();
				}
			
			if (m_status == TankStatus.MOVING)
				moveTankForward();
			
		}
		
		private function oppositeDirection(direction:String):String
		{
			var oppositeDirection:String;
			
			if (direction == Direction.UP)
				oppositeDirection = Direction.DOWN;
			if (direction == Direction.DOWN)
				oppositeDirection = Direction.UP;
			if (direction == Direction.LEFT)
				oppositeDirection = Direction.RIGHT;
			if (direction == Direction.RIGHT)
				oppositeDirection = Direction.LEFT;
			
			return oppositeDirection;
			
		}
		
		private function moveInOppositeDirection():void
		{					
			var currentDirection:String = getDirectionMoving();
			var newDirection:String = oppositeDirection(currentDirection);
			setDirectionFacing(newDirection);
			m_status = TankStatus.MOVING;
		}
		
		public function collisionHandling():void
		{
			var typesCollidedWith:Array = getCollisions();
			for each (var typeCollidedWith:Entity in typesCollidedWith)
			{
				if (typeCollidedWith.type == ObjectTypes.LEVEL)
					LevelCollisionHandling();
				if (typeCollidedWith.type == ObjectTypes.ENEMY_TANK)
				{	
					var tankCollision:BaseCollision = new BaseCollision();
					var currentDirection:String = getDirectionMoving();
					m_currentLocation = tankCollision.pointAfterCollision(currentDirection, height, width, m_currentLocation, height, width);
					//moveInRandomDirection();	
					moveInOppositeDirection();
				}
				if (typeCollidedWith.type == ObjectTypes.BULLET)
					m_status = TankStatus.DEAD;
			}
			
		}
		
		private function moveInRandomDirection():void
		{
			var numberOfDirections:uint = 4;
			var randomNumber:uint = Math.floor( Math.random() * numberOfDirections);
			//trace(randomNumber);
			var newDirection:String;
			switch(randomNumber)
			{
				case 1:
				newDirection = Direction.UP;
				break;
				case 2:
				newDirection = Direction.DOWN;
				break;
				case 3:
				newDirection = Direction.LEFT;
				break;
				case 4:
				newDirection = Direction.RIGHT;
				break;
				default:
				newDirection = getDirectionMoving();
				break;
			}
			
			setDirectionFacing(newDirection);
				
			
		}
		
		private function ShootCondition():void
		{
			var chanceOfFiring:uint = 10;
			var randomNumber:uint = Math.floor( Math.random() * chanceOfFiring);
			if (randomNumber == 5)
				Shoot();	
			
		}
		
		private function SetRandomCourse():void
		{
			var chanceOfChangingDirection:uint = 100;
			var randomNumber:uint = Math.floor( Math.random() * chanceOfChangingDirection);
			if (randomNumber <= 2 )
				moveInRandomDirection();
			
		}
		
		override public function update():void 
		{
			if (m_status != TankStatus.DEAD)
			{
				collisionHandling();
				moveControl();
				SetRandomCourse();
				ShootCondition();
				super.update();
			}

		}
		
	}
	
}